<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

$aCamForm = array();
require("_inc/const.campiGrut.php");

if (reqVar('azione') == 'salva') {
	$msgPag = conferma($aCamForm);
} 

$sqlSel = "SELECT u.* FROM ovf_utenti u "
	." ORDER BY u.cod_utente";
$objDB->exec($sqlSel);
while($aRow = $objDB->fetch()) {
	reqDef('cbx_ut_'.$aRow['id_utente'],'');
	reqDef('cod_ut_'.$aRow['id_utente'],$aRow['cod_utente']);
	reqDef('des_ut_'.$aRow['id_utente'], $aRow['cognome']." ".$aRow['nome']);
}

$sqlSel = "SELECT g.* FROM ovf_gruppi g "
	." ORDER BY g.cod_gruppo";
$objDB->exec($sqlSel);
while($aRow = $objDB->fetch()) {
	reqDef('cbx_gr_'.$aRow['id_gruppo'],'');
	reqDef('cod_gr_'.$aRow['id_gruppo'],$aRow['cod_gruppo']);
	reqDef('des_gr_'.$aRow['id_gruppo'], $aRow['descr']);
}

$sCmdR = "";
$sCmdR .= "<a class=\"cmd1\" href=\"javascript:document.frm1.submit();\" >salva</a>";
$sCmdR .= cmdTorna('torna');

testaHtml('Gestione gruppi utenti','Nuovo gruppi utenti',$msgPag,'gest');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','salva');?>
<?=getHtmlHidden('xsn',XSN);?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Gruppo utenti</td>
   </tr>
<?
echo getHtmlCampiForm($aCamForm);
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="lab1" align="center" colspan='3'>Utenti</td>
   </tr>
<?php 
foreach($_REQUEST as $nX => $vX) {
	if (substr($nX,0,7)!='cbx_ut_') continue;
	$idX = substr($nX,7);
?>
   <tr>
      <td class="txt1" align="center" width='1%'><?php echo getHtmlCheckBox('cbx_ut_'.$idX, 'S');?></td>
      <td class="txt1" align="left" width='10%'>
      	<?php echo getHtmlHidden('cod_ut_'.$idX);echo reqVar('cod_ut_'.$idX)?>
      </td>
      <td class="txt1" align="left" width='89%'>
      	<?php echo getHtmlHidden('des_ut_'.$idX);echo reqVar('des_ut_'.$idX)?>
      </td>
   </tr>
<?php 
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="lab1" align="center" colspan='3'>Gruppi inclusi</td>
   </tr>
<?php 
foreach($_REQUEST as $nX => $vX) {
	if (substr($nX,0,7)!='cbx_gr_') continue;
	$idX = substr($nX,7);
?>
   <tr>
      <td class="txt1" align="center" width='1%'><?php echo getHtmlCheckBox('cbx_gr_'.$idX, 'S');?></td>
      <td class="txt1" align="left" width='10%'>
      	<?php echo getHtmlHidden('cod_ut_'.$idX);echo reqVar('cod_gr_'.$idX)?>
      </td>
      <td class="txt1" align="left" width='89%'>
      	<?php echo getHtmlHidden('des_ut_'.$idX);echo reqVar('des_gr_'.$idX)?>
      </td>
   </tr>
<?php 
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();
function conferma($aCamForm) {
	global $objDB;
	$msg = '';
	$_REQUEST['cod_gruppo'] = strtoupper(trim(reqVar('cod_gruppo')));
	if (reqVar('cod_gruppo')=='') {
		$msg .= "Codice obbligatorio<br>";
	}
	if (preg_match("/[^-_A-Z0-9]/",reqVar('cod_gruppo'))) {
		$msg = "Codice '".reqVar('cod_gruppo')."' puo' contenere solo lettere numeri '-' e '_'<br>";
	}
	if (trim(reqVar('descr'))=='') {
		$msg .= "Descrizione obbligatoria<br>";
	}
	$sqlSel = "SELECT id_gruppo FROM ovf_gruppi " .
			" WHERE cod_gruppo = '".reqVarAS('cod_gruppo')."'";
	$objDB->exec($sqlSel,'insIst');
	$aRow = $objDB->fetch('insIst');
	if ($aRow) {
		$msg .= "Codice gia' esistente<br>";
	}
	if ($msg != '') {
		return $msg;
	}
	$sSqlIns = "INSERT INTO ovf_gruppi SET ";
	foreach($aCamForm as $rX => $aRX) {
		foreach($aRX as $jX => $aCX) {
			if ($aCX[0]=='password') continue;
			$sSqlIns .= $aCX[0]." = '".reqVarAS($aCX[0])."', ";
		}
	}
	$sSqlIns = substr($sSqlIns,0,-2);
	$objDB->exec($sSqlIns,'insRec');
	$newId = $objDB->lastInsertId('insRec');
	
	foreach($_REQUEST as $nX => $vX) {
		if (substr($nX,0,7)!='cbx_ut_') continue;
		if ($vX != 'S') continue;
		$idX = substr($nX,7);
		$sSqlUpd = "INSERT INTO ovf_utenti_gruppi SET id_gruppo = ".$newId
			.", id_utente = $idX";
		$objDB->exec($sSqlUpd);
	}

	foreach($_REQUEST as $nX => $vX) {
		if (substr($nX,0,7)!='cbx_gr_') continue;
		if ($vX != 'S') continue;
		$idX = substr($nX,7);
		$sSqlUpd = "INSERT INTO ovf_gruppi_ricors SET id_gruppo_dest = ".$newId
			.", id_gruppo_orig = $idX";
		$objDB->exec($sSqlUpd);
	}
	
	header("Location: ".urlTorna("gruppo '".reqVar('cod_gruppo')."' inserito"));
	exit();
}
?>